/*
 * Copyright 2016. chenshufei
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package sz.szsmk.citizencard.ui.dzsmkfragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.fragment.app.Fragment;

import org.greenrobot.eventbus.EventBus;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.mvp.BasePresenter;
import sz.szsmk.citizencard.mvp.MvpView;
import sz.szsmk.citizencard.util.PubUtils;


/**
 * <br /> author: chenshufei
 * <br /> date: 16/10/26
 * <br /> email: chenshufei2@sina.com
 */
public class BaseFragment extends Fragment implements MvpView {


    private BasePresenter[] mPresenters;
    protected EventBus eventBus;

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mPresenters = initPresenters();
        //将attachView放在base中搞定
        if (null != mPresenters && mPresenters.length > 0) {
            for (BasePresenter presenter : mPresenters) {
                if (this instanceof MvpView) {
                    presenter.attachView((MvpView) this);
                }
            }
        }

//        eventBus = EventBus.getDefault();
//        if (!EventBus.getDefault().isRegistered(this)) {
//            eventBus.register(this);
//        }
//        eventBus.register(getActivity());

    }

    /**
     * 子类根据实现情况，重写此方法提供presenter出去
     * 若比较简单的页面，直接写死说明，从需要外部数据时，就不需要重写此方法
     *
     * @return
     */
    protected BasePresenter[] initPresenters() {
        return null;
    }


    @Override
    public void onResume() {
        super.onResume();
        if (null != mPresenters && mPresenters.length > 0) {
            for (BasePresenter presenter : mPresenters) {
                presenter.onResumeView();
            }
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (null != mPresenters && mPresenters.length > 0) {
            for (BasePresenter presenter : mPresenters) {
                presenter.onPauseView();
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
//        eventBus.unregister(this);
        if (null != mPresenters && mPresenters.length > 0) {
            for (BasePresenter presenter : mPresenters) {
                presenter.detachView();
            }
        }
    }

    public void launch(Class<? extends Activity> clazz) {
        launch(new Intent(getActivity(), clazz));
    }

    public void launch(Intent intent) {
        startActivity(intent);
    }

    @Override
    public void onUnLogin() {
        PubUtils.popTipOrWarn(getActivity(), getString(R.string.qr_unlogin));
    }


}
